package dominion.player;

import java.util.List;

import dominion.CardTypes;
import dominion.Dominion;
import dominion.Player;
import dominion.Tresure;
import dominion.Victory;

public class TresureOnlyPlayer extends AbstractPlayer {
	public TresureOnlyPlayer(Dominion dominion) {
		super("TresureOnly", dominion);
	}

	public TresureOnlyPlayer(String name, Dominion dominion) {
		super(name, dominion);
	}

	@Override
	public int buy(Dominion dominion, int value) {
		int[] tmp = { 0 };
		if (7 < value && dominion.buy(this, Victory.PROVINCE)) {
			value -= 8;
		} else if (buy(value, dominion, tmp)) {
			value -= tmp[0];
		} else if (2 < value && coinRatio() < 1.5d) {
			dominion.buy(this, Tresure.SILVER);
			value -= 3;
		}
		return value;
	}

	private boolean buy(int current, Dominion dominion, int[] tmp) {
		if (12 < current && current < 16 && dominion.buy(this, Victory.DUCHY)) {
			tmp[0] = 5;
			return true;
		} else if (4 < current && current < 8) {
			if (dominion.limit(CardTypes.Province) < dominion.players().size()
					&& vpdiff(dominion) < 6
					&& dominion.buy(this, Victory.DUCHY)) {
				tmp[0] = 5;
				return true;
			}
			if (5 < current) {
				if (dominion.buy(this, Tresure.GOLD)) {
					tmp[0] = 6;
					return true;
				}
			}
		}
		return false;
	}

	private int vpdiff(Dominion dominion) {
		// solo play
		if (dominion.players().size() < 2) {
			return Victory.DUCHY.value();
		}
		List<Player> rank = dominion.vpRank();
		Player first = rank.get(0);
		Player second = null;
		if (first.equals(this)) {
			second = rank.get(1);
		} else {
			second = this;
		}
		return first.victory() - second.victory();
	}

}